Repository navigation
feat(runtime): report native memory held by wrappers to V8, forward memory pressure - #507
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to This change adds native-memory accounting for wrappers and forwards memory-pressure events to running isolates. No concrete defect was confirmed. A possible edge case remains unconfirmed: negative or failed Pre-merge checks ·
|
…emory pressure V8 only weighs its own heap (plus ArrayBuffer backing stores) when scheduling collections, so a JS wrapper holding megabytes of native memory looks like a few dozen bytes and can sit uncollected until the process is jetsammed. - Wrappers can carry an ExternalMemoryCharge (a v8::ExternalMemoryAccounter) that is returned whenever the wrapper is deleted, from any thread; once the isolate's gate is closed the decrease is skipped since disposal drops it. - interop.alloc(n) charges n bytes to the returned pointer. - Objects JS likely owns (JS constructors, initializers, +1 returns, class factory methods) get an estimated size: NSData length, UIImage/CGImage bitmap size, CVPixelBuffer data size. CG/CV are resolved at runtime, so no new link dependencies. NSDataAdapter is excluded (V8 already counts it). - interop.setExternalSize(obj, bytes) / interop.getExternalSize(obj) let libraries declare the native footprint of what they wrap. - A process-wide dispatch memory-pressure source forwards normal/warn/critical to every live isolate via Isolate::MemoryPressureNotification.
caaee30 to
061eae8
Compare
What
The runtime never told V8 about native memory kept alive by JS wrappers. V8 only weighs its own heap (plus ArrayBuffer backing stores, which it counts itself) when scheduling GCs. So a wrapper holding a multi-MB
UIImagelooks like ~50 bytes and can sit uncollected until iOS jetsams the app. This PR adds reporting at three levels of confidence and forwards OS memory pressure.Accounting primitive
ExternalMemoryCharge(wrappingv8::ExternalMemoryAccounter, the non-deprecated replacement forAdjustAmountOfExternalAllocatedMemory) hangs offBaseDataWrapper. Whichever path deletes the wrapper (finalizer,__releaseNativeCounterpart, teardown, an adapter's-deallocon another thread) returns the charge.Tier 1: exact sizes the runtime allocates
interop.alloc(n)chargesnbytes to the adopted pointer.Tier 2: estimated sizes for objects JS likely owns
NSData→length;UIImage/CGImage→bytesPerRow * height;CVPixelBuffer→CVPixelBufferGetDataSize. They are resolved once per class and cached inCaches.dlsym, so the runtime gains no link dependencies.NSDataAdapteris excluded, since V8 already counts its JS buffer.-CGImagerasterizes them.new X(...)copy/new/Create)view.image) are not charged, because the receiver usually keeps the object alive and the GCs triggered by charging it could not free anything.Tier 3: library-declared sizes
interop.setExternalSize(obj, bytes)replaces the charge on a native object, pointer, reference, block or function reference; 0 clears it.interop.getExternalSize(obj)reads it back.TypeError, because they live as long as the isolate and the charge could never be returned.TypeErrortoo. Since perf(runtime): build struct objects from per-type templates with GC-owned storage #499 their wrapper is destroyed by the storage's backing-store deleter, which may run off-thread and must not touch V8, and V8 already counts that storage.[0, 2^34], otherwiseRangeError. V8 aborts the process, rather than throwing, on a single adjustment above its sanity limit.@nativescript/coreImageSource, canvas and plugins, which know their real footprint.Memory pressure
DISPATCH_SOURCE_TYPE_MEMORYPRESSUREsource maps normal/warn/critical toMemoryPressureLevel::kNone/kModerate/kCriticaland callsIsolate::MemoryPressureNotificationon every live isolate.Known limitations
NSMutableDatagrowth is not tracked.interop.bufferFromDataon a chargedNSDatacounts the bytes twice: once for the NSData wrapper, once for the ArrayBuffer.interop.setExternalSize/getExternalSizetypings belong in@nativescript/typesand are a follow-up.Tests
TestRunner/app/tests/ExternalMemoryTests.js(12 specs) covers:interop.allocRebased on #499. Full suite under ASan on a dedicated simulator: 1835 specs, 0 failures, no sanitizer reports. The memory-pressure path has no automated test because the simulator has no reliable way to raise a memory-pressure event.
Summary by CodeRabbit
New Features
Tests